// pages/inpatient-select/inpatient-select.js
Page({
  	data: {
		inpatientList: [
			{
				id: 1,
				name: '牛韦',
				status: '住院中',
				inpatientNumber: '390090',
				medicalCardNumber: '20209000709',
				admissionTime: '2020/10/09 09:00:00',
				dischargeTime: '2020/10/09 09:00:00'
			}
		],
		selectedInpatient: null
	},

  onLoad(options) {
    console.log('住院人员选择页面加载', options);
    // 如果有传递的住院人ID，自动选中
    if (options.inpatientId) {
      const inpatient = this.data.inpatientList.find(item => item.id == options.inpatientId);
      if (inpatient) {
        this.setData({
          selectedInpatient: inpatient
        });
      }
    }
  },

  // 选择住院人
  selectInpatient(e) {
    const inpatientId = e.currentTarget.dataset.id;
    const inpatient = this.data.inpatientList.find(item => item.id == inpatientId);
    
    this.setData({
      selectedInpatient: inpatient
    });

    console.log('选择了住院人:', inpatient);
  },

  	// 确认选择
	confirmSelection() {
		if (!this.data.selectedInpatient) {
			wx.showToast({
				title: '请先选择住院人',
				icon: 'none',
				duration: 2000
			});
			return;
		}

		console.log('确认选择住院人:', this.data.selectedInpatient);

		// 跳转到住院人信息详情页面，传递住院人信息
		wx.navigateTo({
			url: `/pages/inpatient-detail/inpatient-detail?inpatientId=${this.data.selectedInpatient.id}&inpatientName=${this.data.selectedInpatient.name}&inpatientNumber=${this.data.selectedInpatient.inpatientNumber}`
		});
	},

  // 添加住院人
  addInpatient() {
    wx.showToast({
      title: '添加住院人功能开发中',
      icon: 'none',
      duration: 2000
    });
    
    // 这里可以跳转到添加住院人页面
    // wx.navigateTo({
    //   url: '/pages/add-inpatient/add-inpatient'
    // });
  },

  onShow() {
    console.log('住院人员选择页面显示');
  }
});
